package com.hnkywl.sts.service.organ;

import com.hnkywl.sts.dao.organ.LjwhKclsDao;
import com.hnkywl.sts.entity.organ.Lj;
import com.hnkywl.sts.service.sys.YgService;
import com.hnkywl.sts.util.BaseUtils;
import com.hnkywl.sts.util.ReflectionUtils;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-6-4
 * Time: 上午12:03
 * To change this template use File | Settings | File Templates.
 */
@Service
public class LjwhKclsService {
    private LjwhKclsDao ljwhKclsDao;

    //查询财务收付列表
    public String findAllLjwhKcls(String jyjg, Long jyjgId,int pageNo, int pageSize) {
        if("".equals(jyjg)){
            jyjgId = null;
        }
        Page<Lj> ljwhKclsPage = this.ljwhKclsDao.findLjKclsList(jyjg,jyjgId,pageNo, pageSize);;
        List<Lj> ljwhKclsList = ljwhKclsPage.getResult() == null ? new ArrayList<Lj>() : ljwhKclsPage.getResult();
        String ljwhKclsJson = BaseUtils.fetchCollectionToJson(ljwhKclsList, "id","mc", "jyjg.id", "jyjg.qc", "ll", "hkq", "jsfs.z","sfkjxy","sfdj", "zt");
        return "{\"total\":" + ljwhKclsPage.getTotalCount() + ",\"rows\":[" + ljwhKclsJson + "]}";
    }

    @Transactional
    public String findLjwhKcls(Long jyjgId) {
        List<Lj> ljwhKclsList = ljwhKclsDao.findLjwhKcls(jyjgId);
        String ljwhKclsJson = BaseUtils.fetchCollectionToJson(ljwhKclsList, "id", "mc","sl","ll");
        return "[" + ljwhKclsJson + "]";
    }

    //添加库存零售路径
    @Transactional
    public void addLjKcls(Lj lj) {
        lj.setDateCreated(new Date());
        lj.setLx(4);
        lj.setLl(0.0d);
        lj.setZt("ybc");
        lj.setCreateUser(YgService.getCurrentYg());
        lj.setCreateOrgan(YgService.getCurrentYg().getZggs());
        lj.setCreateDepartment(YgService.getCurrentYg().getZgjg());
        this.ljwhKclsDao.save(lj);
    }

    //修改库存零售路径
    @Transactional
    public void updateLjKcls(Lj lj) {
        Lj saveDate = this.ljwhKclsDao.get(lj.getId());
        saveDate = (Lj) ReflectionUtils.getInstance(saveDate, lj);
        saveDate.setLastUpdated(new Date());
        saveDate.setLl(0.0d);
        saveDate.setSfdj(true);
        saveDate.setZt("ybc");
        this.ljwhKclsDao.save(saveDate);
    }

    //修改库存零售路径
    @Transactional
    public void commitLjKcls(Long ljId) {
        Lj saveDate = this.ljwhKclsDao.get(ljId);
        saveDate.setLastUpdated(new Date());
        saveDate.setZt("ysp");
        this.ljwhKclsDao.save(saveDate);
    }

    //删除库存零售路径
    @Transactional
    public void deleteLjKcls(Long ljId) {
        Lj lj =ljwhKclsDao.get(ljId);
        lj.setDeleted(1);
        lj.setLastUpdated(new Date());
        this.ljwhKclsDao.save(lj);
    }


    //启用路径
    @Transactional
    public void startLj(Long id) {
        Lj lj = ljwhKclsDao.get(id);
        lj.setLastUpdated(new Date());
        lj.setSfdj(false);
        this.ljwhKclsDao.save(lj);
    }

    //停用路径
    @Transactional
    public void stopLj(Long id) {
        Lj lj = ljwhKclsDao.get(id);
        lj.setLastUpdated(new Date());
        lj.setSfdj(true);
        this.ljwhKclsDao.save(lj);
    }

    //路径可修改
    @Transactional
    public void startLjUpdate(Long id) {
        Lj lj = ljwhKclsDao.get(id);
        lj.setLastUpdated(new Date());
        lj.setKxg(true);
        this.ljwhKclsDao.save(lj);
    }

    //路径不可修改
    @Transactional
    public void stopLjUpdate(Long id) {
        Lj lj = ljwhKclsDao.get(id);
        lj.setLastUpdated(new Date());
        lj.setKxg(false);
        this.ljwhKclsDao.save(lj);
    }

    public boolean ajaxValidate(Long id) {
          return ljwhKclsDao.checkUnique(id);
    }

    //查询路径明细
    public Lj find(Long id) {
        return this.ljwhKclsDao.get(id);
    }

    @Autowired
    public void setLjwhKclsDao(LjwhKclsDao ljwhKclsDao) {
        this.ljwhKclsDao = ljwhKclsDao;
    }
}
